package com.web.mall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.mall.pojo.*;
import com.web.mall.pojogroup.TbSpecification;
import com.web.mall.service.SpecificationManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SpecificationManagerController {

    @Autowired
    private SpecificationManagerService specificationManagerService;

    @GetMapping("/specificationListByPage")
    public PageResp<TbBrand> listByPage(PageReq pageReq){
        PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
        Page<Specification> page = (Page<Specification>) specificationManagerService.querySpecification();

        PageResp pageResp = new PageResp();
        pageResp.setTotal(page.getTotal());
        pageResp.setList(page.getResult());

        return pageResp;
    }

    @DeleteMapping("/deleteSpecification/{id}")
    public Result deleteSpecification(@PathVariable Long id){
        boolean b= this.specificationManagerService.deleteSpecification(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/saveSpecification")
    public Result save(@RequestBody TbSpecification specification){
        System.out.println(specification.getSpecification().getId());
        boolean b= this.specificationManagerService.saveSpecification(specification);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/findTbSpecificationById/{id}")
    public Result findTbSpecificationById(@PathVariable Long id){
        TbSpecification tbSpecificationById = this.specificationManagerService.findTbSpecificationById(id);

        return Result.ok(tbSpecificationById);
    }

    @GetMapping("/findSpecificationList")
    public List<Specification> findSpecificationList(){
        return specificationManagerService.findSpecificationList();
    }





}
